import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/storage'

import Login from '@/views/login/login'
import Home from '@/views/layout/index.vue'
import Page404 from '@/views/404'

// import HomeMain from '@/views/layout/home'
// import AddArticle from '@/views/layout/articles/add'
// import Content from '@/views/layout/articles/content'
// import Comment from '@/views/layout/comment'
// import Fans from '@/views/layout/fans'
// import Picture from '@/views/layout/picture'
// import User from '@/views/layout/user'
// import EditArticle from '@/views/layout/articles/editArticle'

const HomeMain = () => import('@/views/layout/home')
const AddArticle = () => import('@/views/layout/articles/add')
const Content = () => import('@/views/layout/articles/content')
const Comment = () => import('@/views/layout/comment')
const Fans = () => import('@/views/layout/fans')
const Picture = () => import('@/views/layout/picture')
const User = () => import('@/views/layout/user')
const EditArticle = () => import('@/views/layout/articles/editArticle')

Vue.use(VueRouter)

const routes = [
  { name: 'Login', path: '/login', component: Login },
  {
    name: 'Home',
    path: '/',
    component: Home,
    children: [
      { name: 'HomeMain', path: '/', component: HomeMain },
      { name: 'AddArticle', path: '/addarticle', component: AddArticle },
      { name: 'Content', path: '/content', component: Content },
      { name: 'Comment', path: '/comment', component: Comment },
      { name: 'Fans', path: '/fans', component: Fans },
      { name: 'Picture', path: '/picture', component: Picture },
      { name: 'User', path: '/user', component: User },
      { name: 'EditArticle', path: '/editarticle:id', component: EditArticle }
    ]
  },
  { name: 'page404', path: '*', component: Page404 }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login' || getToken()) {
    next()
  } else {
    next('/login')
  }
})

export default router
